package com.ts.web.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;


import com.ts.common.util.HibernateSessionFactory;
import com.ts.common.util.bean.Admin;
import com.ts.common.util.bean.Cases;
import com.ts.common.util.bean.Itemmm;
import com.ts.common.util.bean.Kehu;
import com.ts.common.util.bean.Orders;



import com.ts.common.util.bean.News;
import com.ts.web.dao.ICommonDao;

public class CommonDao implements ICommonDao {

	public Admin getAdminByUsername(String username) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		String hql = "from Admin where username='" + username + "'";
		Query query = session.createQuery(hql);

		return (Admin) query.uniqueResult();
	}

	public void saveOrUpdateNews(News news) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		session.clear();
		session.saveOrUpdate(news);
	}

	public List<News> getNews(int count, String type) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		String hql = "from News  where shanchu='1' and type='" + type
				+ "' order by phtime desc";
		Query query = session.createQuery(hql);
		if (count != 0) {
			query.setMaxResults(count);
		}
		return query.list();
	}

	public News getNews(String id) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		String hql = "from News  where id= '" + id + "'";
		Query query = session.createQuery(hql);

		return (News) query.uniqueResult();
	}

	public void saveOrUpdateCase(Cases cases) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		session.clear();
		session.saveOrUpdate(cases);
	}

	public Cases getCases(String id) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		String hql = "from Cases  where id= '" + id + "'";
		Query query = session.createQuery(hql);

		return (Cases) query.uniqueResult();
	}

	public List<Cases> getCases(int cc) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		String hql = "from Cases  where shanchu='1' and cc='0' order by phtime desc";
		Query query = session.createQuery(hql);
		
		return query.list();
	}

	public int getTotalRowsByCases(int cc) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		String hql = "from Cases  where shanchu='1' and cc='0'";
		Query query = session.createQuery(hql);
		//query.setCacheable(true);
		//int totalRows = Integer.parseInt(query.list().get(0).toString());
		List<Cases> list =null;
		int totalRows = 0;
		list = query.list();
		for(Cases cases:list){
			totalRows++;
		}
		
		return totalRows;
	}

	public void saveOrUpdateItems(Itemmm itemmm) throws Exception {
		// TODO Auto-generated method stub
	
			Session session = HibernateSessionFactory.getSession();
			session.clear();
			session.saveOrUpdate(itemmm);
		
	}

	public Itemmm getItemmm(String id) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		String hql = "from Itemmm  where id= '" + id + "'";
		Query query = session.createQuery(hql);

		return (Itemmm) query.uniqueResult();
	}

	public void deleteOrUpdateItems(Itemmm itemmm) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		
		session.delete(itemmm);
	}

	public List<Itemmm> getItemmm() throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		String hql = "from Itemmm   order by cc";
		Query query = session.createQuery(hql);
		
		return query.list();
	}

	public List<Cases> getCase(String itemmmid) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		String hql = "from Cases  where shanchu='1' and itemmmid = '"+itemmmid+"' order by phtime desc";
		Query query = session.createQuery(hql);
		
		return query.list();
	}

	public Itemmm getItemmmOne() throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		String hql = "select top 1 *from Cases  ";
		Query query = session.createQuery(hql);

		return (Itemmm) query.uniqueResult();
	}

	public void deleteOrUpdateCase(Cases cases) throws Exception {
		// TODO Auto-generated method stub
        Session session = HibernateSessionFactory.getSession();
		
		session.delete(cases);
	}

	public void deleteOrUpdateNews(News news) throws Exception {
		// TODO Auto-generated method stub
		 Session session = HibernateSessionFactory.getSession();
			
			session.delete(news);
	}

	public void saveOrUpdateOrders(Orders orders) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		session.clear();
		session.saveOrUpdate(orders);
	}

	public Kehu getKehuByUsername(String username) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		String hql = "from Kehu where username='" + username + "'";
		Query query = session.createQuery(hql);
	
		return (Kehu) query.uniqueResult();
	}

	public News getNewsByUsername(String username) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		String hql = "from News where username='" + username + "'";
		Query query = session.createQuery(hql);
	
		return (News) query.uniqueResult();
	}

	public void saveOrUpdateKehu(Kehu kehu) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		session.clear();
		session.saveOrUpdate(kehu);
	}

	public List<Kehu> getKehu() throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		String hql = "from Kehu order by phtime desc";
		Query query = session.createQuery(hql);
		
		return query.list();
	}

	public Kehu getKehuById(int id) throws Exception {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		String hql = "from Kehu where id='" + id + "'";
		Query query = session.createQuery(hql);
	
		return (Kehu) query.uniqueResult();
	}

	public void deleteOrUpdateKehu(Kehu kehu) throws Exception {
		// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
		
		session.delete(kehu);
	}

}
